﻿<Serializable()>
Public Class clsAlert

#Region "property"

    ''' <summary>
    ''' if alert works
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property AlertOn As Boolean
        Get
            Return m_AlertOn
        End Get
        Set(ByVal value As Boolean)
            m_AlertOn = value
        End Set
    End Property

    Public Property UpperProfitPercentage As Double
        Get
            Return m_UpperProfitPercentage
        End Get
        Set(ByVal value As Double)
            m_UpperProfitPercentage = value
        End Set
    End Property

    Public Property LowerProfitPercentage As Double
        Get
            Return m_LowerProfitPercentage
        End Get
        Set(ByVal value As Double)
            m_LowerProfitPercentage = value
        End Set
    End Property

    ''' <summary>
    ''' set or get user set stocks data
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property MyStocks As System.Collections.ObjectModel.Collection(Of clsMyStockInfo)
        Get
            Return m_myStocks
        End Get
        Set(ByVal value As System.Collections.ObjectModel.Collection(Of clsMyStockInfo))
            m_myStocks = value
        End Set
    End Property


#End Region

#Region "method"
    ''' <summary>
    ''' set current price and return if is' fit the alert setting
    ''' </summary>
    ''' <param name="currentPrice"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SetCurrentPrice(ByVal currentPrice As Double) As Boolean
        If AlertDetection(currentPrice) Then
            RaiseEvent Alert(Me)
            Return True
        End If
        Return False
    End Function
#End Region

#Region "Event"
    Public Event Alert(ByVal sender As Object)

#End Region

    Sub New()
        m_UpperProfitPercentage = 0
        m_LowerProfitPercentage = 0
        m_AlertOn = False
        m_myStocks = New System.Collections.ObjectModel.Collection(Of clsMyStockInfo)()
    End Sub

    Private m_UpperProfitPercentage As Double
    Private m_LowerProfitPercentage As Double
    Private m_AlertOn As Boolean 'if switch on the alert
    Private m_myStocks As System.Collections.ObjectModel.Collection(Of clsMyStockInfo)





    ''' <summary>
    ''' detect if a price alert is ready
    ''' </summary>
    ''' <param name="CurrentPrice"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function AlertDetection(ByVal CurrentPrice As Double) As Boolean
        If m_myStocks Is Nothing Then Return False
        If m_AlertOn = False Then Return False

        Dim MyLastStockInfo As New clsMyStockInfo
        Dim ProfitPercentage As Double

        For Each myStockTmp As clsMyStockInfo In g_MyStocks
            If myStockTmp.DateBuy > MyLastStockInfo.DateBuy Then
                MyLastStockInfo = myStockTmp
            End If
        Next
        ProfitPercentage = (MyLastStockInfo.CostPrice - CurrentPrice) / MyLastStockInfo.CostPrice

        If ProfitPercentage >= 0 Then
            If ProfitPercentage > m_UpperProfitPercentage Then
                Return True
            End If
        Else
            If ProfitPercentage < m_LowerProfitPercentage Then
                Return True
            End If
        End If

        Return False
    End Function
End Class
